package com.example.springinitializaion.controller;

import com.idrsolutions.image.png.PngCompressor;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/ImageCompressController")
public class ImageCompressController {

    @GetMapping("/doComPress")
    public String doComPress(String filePath) throws Exception {

        File file = new File(filePath);
        File outfile=new File("C:\\Users\\滨湖华为\\Desktop\\compress\\"+"1223.png");
        PngCompressor.compress(file,outfile);
        return "success";
    }


    public static void main(String[] args) throws IOException {
//        CompressImage1();
//
//        CompressImage2();

        String str = "https://oa.ypsx-internal.com/weaver/weaver.file.FileDownload?fileid=afa3abe41d1913bf454e7c64343693771abdce40fe948233cef4b58ae953a921b4a74edd74fdd6e55fc949d94fa4e9d8763ded69d584e48ba&download=1&requestid=&ddcode=979f44edf0ca9b2337547b58020d0bce";


        System.out.println(str.length());
    }





    private static void CompressImage2() throws IOException {
        // 指定要压缩的图像文件路径
        File inputFile = new File("C:\\Users\\滨湖华为\\Desktop\\compress\\test11.png");

        // 指定输出图像的路径和文件名
        File outputFile = new File("C:\\Users\\滨湖华为\\Desktop\\compress\\test22.png");

        // 创建Thumbnailator实例，并设置压缩参数
        Thumbnails.of(inputFile)
                .scale(0.5f) // 压缩比例为原始图像的一半
                .outputQuality(0.8f) // 输出图像质量为原始图像的80%
                .toFile(outputFile); // 输出到指定文件
    }


    private static void CompressImage1() throws IOException {
        String filePath = "C:\\Users\\滨湖华为\\Desktop\\compress\\test11.png";
        File file = new File(filePath);
        File outfile=new File("C:\\Users\\滨湖华为\\Desktop\\compress\\test22.png");
        PngCompressor.compress(file,outfile);
    }

}
